﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using TvLibrary.Log;

namespace TvServerPlugin
{
    class Settings
    {
        public static int httpPort;
        public static string webUid;
        public static string webPwd;

        public static bool LoadSettings()
        {
            try
            {

                XmlDocument doc = new XmlDocument();
                if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\TV4Home\\"))
                {
                    try
                    {
                        Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\TV4Home\\");
                    }
                    catch (Exception ex)
                    {

                        Log.Error("TV4Home Error in Create TV4Home Folder" + ex.StackTrace.ToString());
                    }
                }
                if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\TV4Home\\TVServerPlugin\\"))
                {
                    try
                    {
                        Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\TV4Home\\TVServerPlugin\\");
                    }
                    catch (Exception ex)
                    {

                        Log.Error("TV4Home Error in Create TV4Home\\TVServerPlugin folder" + ex.StackTrace.ToString());
                    }
                }
                if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\TV4Home\\TVServerPlugin\\config.xml"))
                {
                    doc.Load(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\TV4Home\\TVServerPlugin\\config.xml");
                    XmlNode gNode = doc.SelectSingleNode("/appconfig/config");
                    httpPort = Int32.Parse(gNode.Attributes["httpport"].Value);
                    webUid = gNode.Attributes["username"].Value;
                    webPwd = gNode.Attributes["password"].Value;
                }


            }
            catch (Exception ex)
            {
                Log.Error("TV4Home: Exception raised while loading settings:" + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace);
                return false;
            }
            return true;
        }

        private static void NewAttribute(XmlNode node, string name, string value)
        {
            XmlAttribute attr = node.OwnerDocument.CreateAttribute(name);
            attr.InnerText = value;
            node.Attributes.Append(attr);
        }
        private static void NewAttribute(XmlNode node, string name, bool value)
        {
            XmlAttribute attr = node.OwnerDocument.CreateAttribute(name);
            if (value)
                attr.InnerText = "1";
            else
                attr.InnerText = "0";
            node.Attributes.Append(attr);
        }
        private static void NewAttribute(XmlNode node, string name, int value)
        {
            XmlAttribute attr = node.OwnerDocument.CreateAttribute(name);
            attr.InnerText = value.ToString();
            node.Attributes.Append(attr);
        }
        private static void AddDbPath(string name, string path, XmlNode dbNode, XmlDocument doc)
        {
            XmlNode node = doc.CreateElement("database");
            NewAttribute(node, "name", name);
            NewAttribute(node, "filename", path);
            dbNode.AppendChild(node);
        }
        private static void AddThumbPath(string name, string path, XmlNode dbNode, XmlDocument doc)
        {
            XmlNode node = doc.CreateElement("thumb");
            NewAttribute(node, "name", name);
            NewAttribute(node, "path", path);
            dbNode.AppendChild(node);
        }
        public static void SaveSettings()
        {
            XmlDocument doc = new XmlDocument();
            XmlNode root = doc.CreateElement("appconfig");
            XmlNode gNode = doc.CreateElement("config");
            NewAttribute(gNode, "httpport", httpPort);
            NewAttribute(gNode, "username", webUid);
            NewAttribute(gNode, "password", webPwd);
            root.AppendChild(gNode);
            doc.AppendChild(root);
            try
            {
                doc.Save(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\TV4Home\\TVServerPlugin\\config.xml");
            }
            catch (Exception ex)
            {

                Log.Error("TV4Home: Error in saving Settings" + ex.StackTrace.ToString());
            
            }
        }
    }
}
